<?php
class ModelSurveyQuestion extends Model {
	
	public function checkAnswer($data) {
				
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "answer WHERE email = '" . $this->db->escape($data['email']) . "' AND survey_id = '" . (int)$data['survey_id'] . "' AND question_id = '" . (int)$data['question_id'] . "' AND language_id = '" . (int)$data['language_id'] . "' LIMIT 1");
		
		return $query->row; 	
	}
	
	public function checkAnswers($data) {
		
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "answer WHERE email = '" . $this->db->escape($data['email']) . "' AND survey_id = '" . (int)$data['survey_id'] . "' AND question_id = '" . (int)$data['question_id'] . "' AND language_id = '" . (int)$data['language_id'] . "'");
		
		return $query->rows; 	
	}
	
	public function editAnswer($data) {
		
		$this->db->query("UPDATE " . DB_PREFIX . "answer SET answer = '" . $this->db->escape($data['answer']) . "', guest_id = '" . (int)$data['guest_id'] . "', customer_id = '" . (int)$data['customer_id'] . "', ip = '" . $this->db->escape($data['ip']) . "', time_took = '" . (int)$data['time_took'] . "', date_modified = '" . $this->db->escape($data['date_modified']) . "' WHERE answer_id = '" . (int)$data['answer_id'] . "'");
			
	}
	
	public function deleteAnswers($data) {
		$this->db->query("DELETE FROM `" . DB_PREFIX . "answer` WHERE email = '" . $this->db->escape($data['email']) . "' AND survey_id = '" . (int)$data['survey_id'] . "' AND question_id = '" . (int)$data['question_id'] . "'");
	}
	
	public function addAnswer($data) {
				
		$this->db->query("INSERT INTO " . DB_PREFIX . "answer SET email = '" . $this->db->escape($data['email']) . "', guest_id = '" . (int)$data['guest_id'] . "', customer_id = '" . (int)$data['customer_id'] . "', survey_id = '" . (int)$data['survey_id'] . "', question_id = '" . (int)$data['question_id'] . "', language_id = '" . (int)$data['language_id'] . "', answer = '" . $this->db->escape($data['answer']) . "', ip = '" . $this->db->escape($data['ip']) . "', time_took = '" . (int)$data['time_took'] . "', date_added = '" . $this->db->escape($data['date_added']) . "', date_modified = '" . $this->db->escape($data['date_modified']) . "'");
		
		$question_id = $this->db->getLastId();
		
		return $question_id; 	
	}
	
	public function getQuestion($question_id) {
		$query = $this->db->query("SELECT DISTINCT * FROM " . DB_PREFIX . "question i LEFT JOIN " . DB_PREFIX . "question_description id ON (i.question_id = id.question_id) LEFT JOIN " . DB_PREFIX . "question_to_store i2s ON (i.question_id = i2s.question_id) WHERE i.question_id = '" . (int)$question_id . "' AND id.language_id = '" . (int)$this->config->get('config_language_id') . "' AND i2s.store_id = '" . (int)$this->config->get('config_store_id') . "' AND i.status = '1'");
	
		return $query->row;
	}
	
	public function getSurveyQuestions($survey_id) {
		
		if ($this->customer->isLogged()) {
			$customer_group_id = $this->customer->getCustomerGroupId();
		} else {
			$customer_group_id = $this->config->get('config_customer_group_id');
		}	
		
		$cache = "sid-".$survey_id;
		
		$survey_data = $this->cache->get('survey.' . $cache . '.' . $customer_group_id);
		
		if (!$survey_data) {		
			
			$sql = "SELECT i.question_id FROM " . DB_PREFIX . "question i LEFT JOIN " . DB_PREFIX . "question_description id ON (i.question_id = id.question_id) LEFT JOIN " . DB_PREFIX . "question_to_store i2s ON (i.question_id = i2s.question_id) WHERE i.survey_id = '" . (int)$survey_id . "' AND id.language_id = '" . (int)$this->config->get('config_language_id') . "' AND i2s.store_id = '" . (int)$this->config->get('config_store_id') . "' AND i.status = '1'";			
			
			$survey_data = array();
			
//			echo $sql;
			
			$query = $this->db->query($sql);
		
			foreach ($query->rows as $result) {
				$survey_data[] = $result['question_id'];
			}
			
			$this->cache->set('survey.' . $cache . '.' . $customer_group_id, $survey_data);
		}
		
		return $survey_data;
	}
	
	public function getQuestions($data) {
		
		if ($this->customer->isLogged()) {
			$customer_group_id = $this->customer->getCustomerGroupId();
		} else {
			$customer_group_id = $this->config->get('config_customer_group_id');
		}	
		
		$cache = md5(http_build_query($data));
		
		$survey_data = $this->cache->get('survey.' . $cache . '.' . $customer_group_id);
		
		if (!$survey_data || 1) {		
			
			$sql = "SELECT * FROM " . DB_PREFIX . "question i LEFT JOIN " . DB_PREFIX . "question_description id ON (i.question_id = id.question_id) LEFT JOIN " . DB_PREFIX . "question_to_store i2s ON (i.question_id = i2s.question_id) WHERE id.language_id = '" . (int)$this->config->get('config_language_id') . "' AND i2s.store_id = '" . (int)$this->config->get('config_store_id') . "' AND i.status = '1'";			
			
			if (isset($data['filter_survey_id'])) {
				$sql .= " AND i.survey_id = '" . (int)$data['filter_survey_id'] . "'";
			}
			
			$sort_data = array(
				'i.sort_order',
				'i.date_added'
			);	
			
			if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
				$sql .= " ORDER BY " . $data['sort'];
			} else {
				$sql .= " ORDER BY i.sort_order";	
			}
			
			if (isset($data['order']) && ($data['order'] == 'DESC')) {
				$sql .= " DESC";
			} else {
				$sql .= " ASC";
			}
		
			if (isset($data['start']) || isset($data['limit'])) {
				if ($data['start'] < 0) {
					$data['start'] = 0;
				}				
	
				if ($data['limit'] < 1) {
					$data['limit'] = 20;
				}	
			
				$sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
			}
			
			$survey_data = array();
			
//			echo $sql;
			
			$query = $this->db->query($sql);
		
			foreach ($query->rows as $result) {
				$survey_data[$result['question_id']] = $this->getQuestion($result['question_id']);
			}
			
			$this->cache->set('survey.' . $cache . '.' . $customer_group_id, $survey_data);
		}
		
		return $survey_data;
	}
	
	public function getQuestionLayoutId($question_id) {
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "question_to_layout WHERE question_id = '" . (int)$question_id . "' AND store_id = '" . (int)$this->config->get('config_store_id') . "'");
		 
		if ($query->num_rows) {
			return $query->row['layout_id'];
		} else {
			return $this->config->get('config_layout_question');
		}
	}	
}
?>